package org.summerframework.component.security.oauth.alipay;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayOpenAuthTokenAppRequest;
import com.alipay.api.response.AlipayOpenAuthTokenAppResponse;
import org.summerframework.component.security.oauth.LoginType;
import org.summerframework.component.security.oauth.OAuthAppIdAndAppSecret;
import org.summerframework.component.security.oauth.OAuthResultCode;
import org.summerframework.component.security.oauth.exception.OAuthException;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝第三方登录无法返回用户信息
 */
public class AlipayThirdPartyOAuthSupper implements AlipayOAuthInterceptor {

    private final AlipayAppIdAndAppSecret alipayAppIdAndAppSecret;

    private AlipayClient alipayClient;

    public AlipayThirdPartyOAuthSupper(AlipayAppIdAndAppSecret alipayAppIdAndAppSecret) {
        this.alipayAppIdAndAppSecret = alipayAppIdAndAppSecret;
        alipayClient = new DefaultAlipayClient(alipayAppIdAndAppSecret.getGateway(), alipayAppIdAndAppSecret.getAppId(), alipayAppIdAndAppSecret.getPrivateKey(), "json", alipayAppIdAndAppSecret.getCharset(), alipayAppIdAndAppSecret.getPublicKey(), alipayAppIdAndAppSecret.getSignType());
    }

    @Override
    public AlipayUserInfo getResultInfo(AlipayAccessToken alipayAccessToken) {
        AlipayUserInfo alipayUserInfo = new AlipayUserInfo();
        AlipayOpenAuthTokenAppRequest request = new AlipayOpenAuthTokenAppRequest();
        Map<String, String> param = new HashMap<>();
        param.put("grant_type", alipayAccessToken.getGrantType());
        param.put("code", alipayAccessToken.getCode());

        request.setBizContent(JSONObject.toJSONString(param));

        try {
            AlipayOpenAuthTokenAppResponse response = alipayClient.execute(request);

            if (!response.isSuccess()) {
                throw new OAuthException(OAuthResultCode.ILLEGAL_ARGUMENT, response.getSubMsg());
            }

            alipayUserInfo.setAuthToken(response.getAppAuthToken());
            alipayUserInfo.setRefreshToken(response.getAppRefreshToken());
            alipayUserInfo.setExpiresIn(response.getExpiresIn());
            alipayUserInfo.setReExpiresIn(response.getReExpiresIn());
            alipayUserInfo.setUserId(response.getUserId());
            alipayUserInfo.setAppId(response.getAuthAppId());
        } catch (AlipayApiException e) {
            throw new OAuthException(OAuthResultCode.ILLEGAL_ARGUMENT, e.getMessage(), e);
        }

        return alipayUserInfo;
    }

    @Override
    public AlipayUserInfo getInfo(AlipayUserInfo alipayUserInfo) {
        return alipayUserInfo;
    }

    @Override
    public OAuthAppIdAndAppSecret getOAuthInfo() {
        return alipayAppIdAndAppSecret;
    }

    @Override
    public boolean supportsType(LoginType loginType) {
        return LoginType.ALIPAY_THIRD_PARTY == loginType;
    }

    @Override
    public AlipayAccessToken getAccessToken(String code) {
        return new AlipayAccessToken(code);
    }
}
